﻿using csDmc1380;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using YJYpublicClass;
using static WindowsFormsApp2.接口类.Wheel;

namespace WindowsFormsApp2.接口类
{
    /// <summary>
    /// 组对控制类
    /// </summary>
    public class AligningControl
    {
        #region 【板卡初始化】
        /// <summary>
        /// 初始化所有控制卡，返回卡数目
        /// </summary>
        /// <returns></returns>
        /// 

        private bool controlInit = false;

        public bool ControlInit { get { return controlInit; } }

        /// <summary>
        /// 初始化板卡，返回板卡数目
        /// </summary>
        /// <returns></returns>
        public int boardInit()
        {
            try
            {

                int r = Dmc1000.d1000_board_init();
                if (r != 0) { controlInit = true; }
                else
                {
                    controlInit = false;
                    System.Windows.Forms.MessageBox.Show(
                                  "板卡初始化失败，请检查硬件连接或驱动",
                                  " ",
                                  System.Windows.Forms.MessageBoxButtons.OK,
                                  System.Windows.Forms.MessageBoxIcon.Information,
                                  System.Windows.Forms.MessageBoxDefaultButton.Button1,
                                  System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly |
                                  System.Windows.Forms.MessageBoxOptions.ServiceNotification);
                }
                return r;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
                return 0;
            }
        }

        /// <summary>
        /// 关闭所有控制卡
        /// </summary>
        /// <returns>正常运行返回0</returns>
        public int boardClose()
        {
            controlInit = false;
            return Dmc1000.d1000_board_close();

        }








        #endregion


        #region 【组对滚轮架控制】
        public bool RunFlagH = false;
        public void startrunH()
        {
            RunFlagH = true;
            Dmc1000.d1000_out_bit(10, 0);


        }
        public void stoprunH()
        {
            RunFlagH = false;
            Dmc1000.d1000_out_bit(10, 1);

        }

        #endregion



        #region 【后方滚轮架控制】
        public bool RunflagB = false;///后方滚轮架开启状态
        public int RunModelB = 1;/// 1：移动模式 2：转动模式
        public int MovestateB = 0; ///0:未移动  1：正移动  2：反移动
        public int SpinstateB = 0; ///0:未转动  1：正转动  2：反转动


        private string ErrMes1 = "后方滚轮架未启动！";

        //启停状态控制
        public int RunStartB()
        {
            //输出启动状态
            int r = Dmc1000.d1000_out_bit(13, 0);
            RunflagB = true;
            RunModelB = 1;
            MovestateB = 0;
            SpinstateB = 0;


            return r;
        }
        public int RunStopB()
        {

            RunflagB = false;
            RunModelB = 1;
            MovestateB = 0;
            SpinstateB = 0;

            //停止所有输出
            Dmc1000.d1000_out_bit(14, 1);
            Dmc1000.d1000_out_bit(15, 1);
            Dmc1000.d1000_out_bit(16, 1);
            Dmc1000.d1000_out_bit(17, 1);
            Dmc1000.d1000_out_bit(18, 1);
            //关闭状态
            return Dmc1000.d1000_out_bit(13, 1);

        }
        public int TurnMoveState()
        {
            if (!RunflagB) { MessageBox.Show(ErrMes1); return -1; }
            RunModelB = 1;
            //左右移动状
            return Dmc1000.d1000_out_bit(14, 0);

        }
        public int TurnSpinState()
        {
            if (!RunflagB) { MessageBox.Show(ErrMes1); return -1; }
            RunModelB = 2;
            //旋转状态
            return Dmc1000.d1000_out_bit(14, 1);

        }

        //移动控制
        public int MovePosiStart()
        {
            if (!RunflagB) { MessageBox.Show(ErrMes1); return -1; }
            if (RunModelB != 1) { MessageBox.Show("请切换到移动状态！"); return -1; }
            MovestateB = 1;
            //正移动
            int r = Dmc1000.d1000_out_bit(15, 0);
            return r;

        }
        public int MovePosiStop()
        {
            if (!RunflagB) { MessageBox.Show(ErrMes1); return -1; }
            if (RunModelB != 1) { MessageBox.Show("请切换到移动状态！"); return -1; }
            MovestateB = 0;
            //正移动停止
            int r = Dmc1000.d1000_out_bit(15, 1);

            return r;
        }
        public int MoveNegaStart()
        {
            if (!RunflagB) { MessageBox.Show(ErrMes1); return -1; }
            if (RunModelB != 1) { MessageBox.Show("请切换到移动状态！"); return -1; }
            MovestateB = 2;
            //反移动
            return Dmc1000.d1000_out_bit(16, 0);

        }
        public int MoveNegaStop()
        {
            if (!RunflagB) { MessageBox.Show(ErrMes1); return -1; }
            if (RunModelB != 1) { MessageBox.Show("请切换到移动状态！"); return -1; }
            MovestateB = 0;
            //反移动停止
            return Dmc1000.d1000_out_bit(16, 1);

        }

        //旋转控制
        public int SpinUpStart()
        {
            if (!RunflagB) { MessageBox.Show(ErrMes1); return -1; }
            if (RunModelB != 2) { MessageBox.Show("请切换到转动状态！"); return -1; }
            SpinstateB = 1;
            //正转
            return Dmc1000.d1000_out_bit(15, 0);
        }
        public int SpinUpStop()
        {
            SpinstateB = 0;
            //正转停止
            return Dmc1000.d1000_out_bit(15, 1);
        }
        public int SpinDownStart()
        {
            if (!RunflagB) { MessageBox.Show(ErrMes1); return -1; }
            if (RunModelB != 2) { MessageBox.Show("请切换到转动状态！"); return -1; }
            SpinstateB = 2;
            //反转
            return Dmc1000.d1000_out_bit(16, 0);

        }
        public int SpinDownStop()
        {
            SpinstateB = 0;
            //反转停止
            return Dmc1000.d1000_out_bit(16, 1);

        }
        public int SpeedUp()
        {
            if (!RunflagB) { MessageBox.Show(ErrMes1); return -1; }

            //通断一次，加一个等级
            Dmc1000.d1000_out_bit(17, 0);
            int r = Dmc1000.d1000_out_bit(17, 1);
            return r;
        }
        public int SpeedDown()
        {
            if (!RunflagB) { MessageBox.Show(ErrMes1); return -1; }
            //通断一次，减一个等级
            Dmc1000.d1000_out_bit(18, 0);

            return Dmc1000.d1000_out_bit(18, 1);

        }


        #endregion



    }
    public class Wheel
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="Pnum"></param>
        /// <param name="Nnum"></param>
        public Wheel(int id, int POutNum, int NOutNum, int zeroInNum, int HardLimitPNum, int HardLimitNNum, double zeroOffset = 200, double softLimitP = 100, double softLimitN = -250)
        {
            ID = id;
            PositiveOutNum = POutNum;
            NegativeOutNum = NOutNum;
            HardLimitNInNum = HardLimitNNum;
            HardLimitPInNum = HardLimitPNum;
            ZeroInNum = zeroInNum;
            SoftLimitP = softLimitP;
            SoftLimitN = softLimitN;
            ZeroPosOffSet = zeroOffset;
        }
        /// <summary>
        /// 滚轮ID 
        /// </summary>
        public int ID;
        /// <summary>
        /// 正向移动对应输出
        /// </summary>
        public int PositiveOutNum;
        /// <summary>
        /// 负向移动对应输出
        /// </summary>
        public int NegativeOutNum;
        /// <summary>
        /// 原点限位输入
        /// </summary>
        public int ZeroInNum;
        /// <summary>
        /// 正硬限位输入
        /// </summary>
        public int HardLimitPInNum;
        /// <summary>
        /// 负硬限位输入
        /// </summary>
        public int HardLimitNInNum;
        /// <summary>
        /// 滚轮各运动状态
        /// </summary>
        public enum RunState { Stop = 0, RunPosit = 1, RunNegat = 2 }
        /// <summary>
        /// 滚轮当前状态
        /// </summary>
        public RunState State;

        public double SoftLimitP;
        public double SoftLimitN;

        public bool SoftLimitPFlag = false;
        public bool SoftLimitNFlag = false;
        public bool GoZeroFlag = false;
        public bool IsOnZero = false;
        /// <summary>
        /// 该轮传感器的原始数据
        /// </summary>
        public double OriginPosition = 0;
        /// <summary>
        /// 触碰原点开关时的原始数据
        /// </summary>
        public double ZeroPos = 0;
        /// <summary>
        /// 原点偏移
        /// </summary>
        public double ZeroPosOffSet = 0;
        /// <summary>
        /// 当前位置
        /// </summary>

        public double CurrentPosition { get { return OriginPosition - ZeroPos - ZeroPosOffSet; } }

        public void StartRunPosit()
        {
            if (SoftLimitPFlag) { MessageBox.Show($"轮{ID}正软限位已触发，无法移动！"); return; }
            if (State != RunState.Stop)
            {
                MessageBox.Show($"轮{ID}处于运动状态，无法启动移动！"); return;
            }
            Dmc1000.d1000_out_bit(PositiveOutNum, 0);
            Thread.Sleep(100);
            Dmc1000.d1000_out_bit(PositiveOutNum, 1);
            Thread.Sleep(100);
            Dmc1000.d1000_out_bit(PositiveOutNum, 0);
            State = RunState.RunPosit;
        }
        public void StopRunPosit()
        {
            Dmc1000.d1000_out_bit(PositiveOutNum, 1);
            State = RunState.Stop;

        }
        public void StartRunNegat()
        {
            if (SoftLimitNFlag) { MessageBox.Show($"轮{ID}负软限位已触发，无法移动！"); return; }
            if (State != RunState.Stop)
            {
                MessageBox.Show($"轮{ID}处于运动状态，无法启动移动！"); return;
            }
            Dmc1000.d1000_out_bit(NegativeOutNum, 0);
            Thread.Sleep(100);
            Dmc1000.d1000_out_bit(NegativeOutNum, 1);
            Thread.Sleep(100);
            Dmc1000.d1000_out_bit(NegativeOutNum, 0);
            State = RunState.RunNegat;

        }
        public void StopRunNegat()
        {
            Dmc1000.d1000_out_bit(NegativeOutNum, 1);
            State = RunState.Stop;
        }

        public void MoveToPos(double Position)
        {
            if (Position > SoftLimitP | Position < SoftLimitN)
            {
                MessageBox.Show($"轮{ID}:目标点位超出限位！");
                return;

            }
            try
            {

                int sensorIndex = ID - 1;

                if (CurrentPosition < Position)//正向移动，（内移动）
                {

                    StartRunPosit();
                    bool ask2 = true;
                    while (ask2)
                    {
                        //实时对比
                        if (CurrentPosition > Position)
                        {
                            StopRunPosit();

                            ask2 = false;
                        }
                        Thread.Sleep(5);
                    }



                }
                else if (CurrentPosition > Position)
                {
                    StartRunNegat();
                    bool ask2 = true;
                    while (ask2)
                    {
                        //实时对比
                        if (CurrentPosition <= Position)
                        {
                            StopRunNegat();
                            ask2 = false;
                        }
                        Thread.Sleep(5);
                    }
                }



            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }

        }
        public void MoveToPosRelatively(double MoveDistance)
        {
            double Position = MoveDistance + CurrentPosition;
            if (Position > SoftLimitP | Position < SoftLimitN)
            {
                MessageBox.Show($"轮{ID}:目标点位超出限位！");
                return;

            }
            try
            {
                int sensorIndex = ID - 1;

                if (CurrentPosition < Position)//正向移动，（内移动）
                {

                    StartRunPosit();
                    bool ask2 = true;
                    while (ask2)
                    {
                        //实时对比
                        if (CurrentPosition > Position)
                        {
                            StopRunPosit();

                            ask2 = false;
                        }
                        Thread.Sleep(5);
                    }



                }
                else if (CurrentPosition > Position)
                {
                    StartRunNegat();
                    bool ask2 = true;
                    while (ask2)
                    {
                        //实时对比
                        if (CurrentPosition <= Position)
                        {
                            StopRunNegat();
                            ask2 = false;
                        }
                        Thread.Sleep(5);
                    }
                }



            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }

        }
        public void AllStop()
        {
            StopRunPosit();
            StopRunNegat();
        }

    }
    // [TypeConverter(typeof(ExpandableObjectConverter))] // 支持展开子属性
    public class AxisSettings
    {
        [Category("起始点设置"), DisplayName("轴1起始点")]
        public double Axis1Start { get; set; }

        [Category("起始点设置"), DisplayName("轴2起始点")]
        public double Axis2Start { get; set; }

        [Category("起始点设置"), DisplayName("轴3起始点")]
        public double Axis3Start { get; set; }

        [Category("起始点设置"), DisplayName("轴4起始点")]
        public double Axis4Start { get; set; }

        [Category("零点设置"), DisplayName("轴1零点")]
        public double Axis1Zero { get; set; }

        [Category("零点设置"), DisplayName("轴2零点")]
        public double Axis2Zero { get; set; }

        [Category("零点设置"), DisplayName("轴3零点")]
        public double Axis3Zero { get; set; }

        [Category("零点设置"), DisplayName("轴4零点")]
        public double Axis4Zero { get; set; }




        [Category("错边计算"), DisplayName("截取点数")]
        public int CapPointNum { get; set; } = 3200;
        [Category("错边计算"), DisplayName("方差核宽度")]
        public int VariWindowWidth { get; set; } = 5;
        [Category("错边计算"), DisplayName("方差阈值")]
        public double VariThreshold { get; set; } = 5;
        [Category("错边计算"), DisplayName("错边报警阈值")]
        public double HeightDisMax { get; set; } = 2;
        [Category("错边计算"), DisplayName("间隙报警阈值")]
        public double GapDisMax { get; set; } = 2;



        [Category("矫正过程"), DisplayName("间隙差值阈值")]
        public double HeightDisThreshold { get; set; } = 2;
        [Category("矫正过程"), DisplayName("错边差值阈值")]
        public double GapDisThreshold { get; set; } = 2;
        [Category("矫正过程"), DisplayName("同侧轮差距报警阈值")]
        public double WheelDisMax { get; set; } = 150;



    }


}
